package core.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public abstract class Converter {
	private static XStream xstream = new XStream(new DomDriver());

	private static String dateFormatPattern = "MM.dd.yyyy HH:mm:ss";
	private static DateFormat dateFormat = new SimpleDateFormat(dateFormatPattern);

	public static String getDateFormatPattern() {
		return dateFormatPattern;
	}
	
	public static String objectToXML(Object obj) {
		if(obj == null) {
			return "";
		}
		return xstream.toXML(obj);
	}

	public static Object XMLToObject(String xml) {
		if(xml == null) {
			return null;
		}
		return xstream.fromXML(xml);
	}

	public static String dateToString(Date date) {
		if(date == null) {
			return "";
		}
		return dateFormat.format(date);
	}

	public static Date stringToDate(String strDate) {
		if(strDate == null) {
			return null;
		}
		try {
			return dateFormat.parse(strDate);
		} catch (ParseException e) {
			return null;
		}
	}
}
